/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	/// <summary>
	/// Provides properties of the Web Services API in use
	/// (Groove platform version and web services version information)
	/// </summary>
	public class VersionProperties
	{
		private GrooveWebServicesV12Helpers.Version m_PlatformVersion;
		private GrooveWebServicesV12Helpers.Version[] m_WebServicesVersions;

		public VersionProperties(GrooveWebServicesV12Helpers.Identity i_Identity)
		{
			//  Get initialized reference to web service
			GroovePropertiesWebService.GrooveProperties PropertiesWebService = GrooveWebServicesV12Helpers.VersionProperties.CreateInitializedPropertiesWebService
				(
				i_Identity.URI,
				GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_PROPERTIES_URI,
				i_Identity.Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to read
			GroovePropertiesWebService.Properties Properties =  PropertiesWebService.Read();

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("PropertiesWebService::Read, " + TimeSpan.TotalMilliseconds + " ms");
			}

			m_PlatformVersion = new GrooveWebServicesV12Helpers.Version
				(
				Properties.Version.Major,
				Properties.Version.Minor,
				Properties.Version.Custom,
				Properties.Version.Sequence
				);

			System.Collections.ArrayList WebServicesVersionArrayList = new System.Collections.ArrayList();
			foreach (GroovePropertiesWebService.Version WebServicesVersion in Properties.WebServicesVersions)
			{
				GrooveWebServicesV12Helpers.Version Version = new GrooveWebServicesV12Helpers.Version
					(
					WebServicesVersion.Major,
					WebServicesVersion.Minor,
					WebServicesVersion.Custom,
					WebServicesVersion.Sequence
					);
				
				WebServicesVersionArrayList.Add(Version); 
			}

			//  Convert array list to array
			m_WebServicesVersions = (GrooveWebServicesV12Helpers.Version[])WebServicesVersionArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Version));
		}

		#region Properties Properties and Events

		public static GroovePropertiesWebService.GrooveProperties CreateInitializedPropertiesWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			if (!i_Context.IsLocal)
				throw new System.Exception("This method can not be remotely executed!");

			//  Create the service
			GroovePropertiesWebService.GrooveProperties PropertiesWebService = new GroovePropertiesWebService.GrooveProperties();

			//  Create the Groove SOAP header
			PropertiesWebService.GrooveRequestHeaderValue = new GroovePropertiesWebService.GrooveRequestHeader();

			//  Set appropriate header values
			PropertiesWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
			PropertiesWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

			//  Set the URL
			PropertiesWebService.Url = i_Context.Host + i_TargetURI;

			//  Set Timeout
			PropertiesWebService.Timeout = i_Context.Timeout;

			return PropertiesWebService;
		}

		public GrooveWebServicesV12Helpers.Version PlatformVersion
		{
			get
			{
				return m_PlatformVersion;
			}
		}

		public GrooveWebServicesV12Helpers.Version[] WebServicesVersions
		{
			get
			{
				return m_WebServicesVersions;
			}
		}

		#endregion
	}
}
